/**
* Base Model will be extended by all components. This class has utility methods and variables which are required at a framework level.
* This file specifies all those functions which are not dependent on [KnockoutJS]{@link http://knockoutjs.com}.
* @module base-model
*/
define(["platform"],
function(Platform) {
"use strict";
/**
* Base Model will be extended by all components. This class has utility methods and variables which are required at a framework level.
* This file specifies all those functions which are not dependent on [KnockoutJS]{@link http://knockoutjs.com}.
* @class
* @alias BaseModel
* @memberof module:base-model
*/
var baseModel = function BaseModel() {
/**
* Assign this to self.
* @member {Object}
*/
var self = this;
/**
* This function trims the payload.
* @function trimPayload
* @memberof BaseModel
* @instance
* @param {String} key The string argument to trim.
* @param {String} value The return value.
* @return {undefined|String} If the argument contains temp_, return undefined else return the argument as is.
*/
self.trimPayload = function(key, value) {
if (key.substring(0, 5) !== "temp_") {
return value;
}
};
/**
* Holds the random number for use by [incrementIdCount]{@linkcode BaseModel#incrementIdCount} and [currentIdCount]{@linkcode BaseModel#currentIdCount}
* @member {Number}
*/
var idCount = 0;
/**
* Creates and returns a random number on each invocation.
* Useful for dynamic id generation for HTML Elements.
* @memberof BaseModel
* @function incrementIdCount
* @instance
* @return {Number} The random number returned.
*/
self.incrementIdCount = function() {
idCount = Math.floor((Math.random() * 10000) + 1);
return idCount;
};
/**
* Returns the random number stored by [idCount]{@linkcode BaseModel~idCount}.
* @memberof BaseModel
* @function currentIdCount
* @instance
* @return {Number} The current value [idCount]{@linkcode BaseModel~idCount} holds.
*/
self.currentIdCount = function() {
return idCount;
};
/**
* This function returns the type of cordova device, i.e. iOS or Android.
* @memberof BaseModel
* @function cordovaDevice
* @instance
* @return {String} The type of cordova device.
*/
self.cordovaDevice = function() {
if (window.cordova && window.device) {
return window.device.platform.toUpperCase();
}
};
/**
* Returns a string corresponding to current device size computed via screen width.
* @function getDeviceSize
* @instance
* @memberof BaseModel
* @return {String} Returns large, medium or small.
*/
self.getDeviceSize = function() {
if (window.innerWidth > 1023) {
return "large";
} else if (window.innerWidth > 767) {
return "medium";
} else {
return "small";
}
};
/**
* In order to show selected description (not the value) in review screen
* we need to keep track of selected descriptions for each dropdown, this function
* will return description based on the key passed.
*
* @function getDescriptionFromCode
* @param {Array} enumArray Array containing all the dropdown values
* @param {String} selectedCode selected code from dropdown
* @param {String} codeString optional argument in case code string in array is something other than default ('code')
* @param {String} descString optional argument in case desc string in array is something other than default ('description')
* @memberof BaseModel
* @instance
* @returns {String} Description of selected value
*/
self.getDescriptionFromCode = function(enumArray, selectedCode, codeString, descString) {
var returnVal = "",
codeStr = codeString || "code",
descStr = descString || "description",
i;
if (enumArray) {
for (i = 0; i < enumArray.length; i++) {
if (enumArray[i][codeStr] === selectedCode) {
returnVal = enumArray[i][descStr];
break;
}
}
}
return returnVal;
};
/**
* This function switches the page using window.location.assign() or window.location.replace().
*
* @function switchPage
* @param {Object} config Object containing the configuration information for switching pages.
* @param {String} config.module This is the module of the page the user will switch to.
* @param {String} config.context This is the context of the page the user will switch to.
* @param {Object} config.homeComponent This object contains information for switching to home components.
* Be forewarned though, if specified, will ignore module and context specified before.
* @param {String} config.homeComponent.component The name of the home component.
* @param {String} config.homeComponent.module The module of the home component.
* @param {Boolean} [isSecure=false] Optional boolean argument that specifies whether the page is secure or public.
* Defaults to false.
* @param {Boolean} [historyRequired=true] Optional boolean argument that specifies whether the page is loaded via assign or replace.
* Defaults to true.
* @memberof BaseModel
* @instance
* @returns {Boolean} Returns false.
* @example
* switchPage({ module: 'login' }, true); // will switch to login page from pre-login screen
*/
self.switchPage = function(config, isSecure, historyRequired) {
isSecure = isSecure || false;
historyRequired = historyRequired || true;
var securePage = "/pages/home.html",
publicPage = "/index.html",
baseLocation = (isSecure ? securePage : publicPage);
var parameters = config;
if (config && config.homeComponent) {
parameters = {
homeComponent: config.homeComponent.component,
homeModule: config.homeComponent.module
};
if (config.homeComponent.query) {
Object.keys(config.homeComponent.query).forEach(function(key) {
parameters[key] = config.homeComponent.query[key];
});
}
}
window.onbeforeunload = null;
Platform.getInstance().then(function(platform) {
var serverURL = platform("getServerURL");
if (serverURL) {
baseLocation = serverURL + (isSecure ? securePage : publicPage);
}
window.location[(historyRequired ? "assign" : "replace")](self.QueryParams.add(baseLocation, parameters));
});
return false;
};
/**
* @summary Utility function that returns a formatted string.
* @description The first argument is a string containing zero or more placeholder tokens.
* Placeholder tokens are { }.
* The next argument should be an Object that contains keys as the tokens used in first argument and values as the values to be substituted.
* This function is commonly used for URL paramterization in [parameterizeURL]{@linkcode BaseService~parameterizeURL}.
* @function format
* @memberof BaseModel
* @instance
* @param {String} message The string containing tokens to be formatted.
* @param {Object.} parameters The object containing key-wise description for tokens used in message.
* @param {Boolean} [isEncoded=false] The boolean specifying whether the key should be encoded using encodeURIComponent or not.
* Defaults to false. Use with caution.
* @returns {String} The formatted string
* @example
* self.format('This string needs to be {token}', { token : 'formatted' });
* // returns "This string needs to be formatted".
*/
self.format = function(message, parameters, isEncoded) {
if (!message) {
throw new ReferenceError("Please specify a valid message");
}
if (parameters) {
var jsonObject = typeof parameters === "string" ? JSON.parse(parameters) : parameters,
allPropertyNames = Object.keys(jsonObject);
for (var i = 0; i < allPropertyNames.length; i++) {
message = message.replace("{" + allPropertyNames[i] + "}", isEncoded ? encodeURIComponent(jsonObject[allPropertyNames[i]]) : jsonObject[allPropertyNames[i]]);
}
}
return message;
};
/**
* Internal function used for obtaining value of nested object's key using string accessor.
* @protected
* @memberof BaseModel
* @function getObjectProperty
* @param {Object} object The object whose value needs to be computed.
* @param {String} stringKey The string value of nested property, e.g. 'a.b.c.d.e'.
* @return {*} The value of the property being accessed.
*/
function getObjectProperty(object, stringKey) {
var latestValue = object;
for (var i = 0, partialKey = stringKey.split("."); i < partialKey.length; i++) {
latestValue = latestValue[partialKey[i]];
}
return latestValue;
}
/**
* Utility function to sort array of objects based on multiple properties.
* @function sortLib
* @memberof BaseModel
* @instance
* @param {Array.